package com.example.zy5_project_jetpack.mvp.view.Adapter;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.zy5_project_jetpack.R;
import com.example.zy5_project_jetpack.mvp.view.Bean.HomeBean;

import java.util.ArrayList;
import java.util.List;

public class HomeRecyclerAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<HomeBean.DataBeanX.DataBean> dataBeans = new ArrayList<>();

    public HomeRecyclerAdapter(Context context) {
        this.context = context;
    }

    public void addList(List<HomeBean.DataBeanX.DataBean> dataBeans) {
        this.dataBeans.addAll(dataBeans);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        HomeBean.DataBeanX.DataBean dataBean = dataBeans.get(position);
        Glide.with(context).load(dataBean.getAuthor().getAvatar()).into(viewHolder.iv_image);
        viewHolder.tv_text.setText(dataBean.getAuthor().getName());
        viewHolder.tv_title.setText(dataBean.getFeeds_text());

        viewHolder.tv_introduce.setText(dataBean.getActivityText());
        viewHolder.view_video.setVideoURI(Uri.parse(dataBean.getUrl()));
        MediaController mediaController = new MediaController(context);
        mediaController.setPrevNextListeners(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("tag","下一集");
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("tag","上一集");
            }
        });
        viewHolder.view_video.setMediaController(mediaController);
        viewHolder.view_video.start();
        viewHolder.view_video.stopPlayback();//停止播放视频，并且释放
        viewHolder.view_video.suspend();//在任何状态下释放媒体资源
    }

    @Override
    public int getItemCount() {
        return dataBeans.size();
    }

    public static
    class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public ImageView iv_image;
        public TextView tv_text;
        public TextView tv_title;
        public VideoView view_video;
        public TextView tv_introduce;
        public TextView radio_zan;
        public TextView radio_step;
        public TextView radio_comment;
        public TextView radio_search;

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.iv_image = (ImageView) rootView.findViewById(R.id.iv_image);
            this.tv_text = (TextView) rootView.findViewById(R.id.tv_text);
            this.tv_title = (TextView) rootView.findViewById(R.id.tv_title);
            this.view_video = (VideoView) rootView.findViewById(R.id.view_video);
            this.tv_introduce = (TextView) rootView.findViewById(R.id.tv_introduce);
            this.radio_zan = (TextView) rootView.findViewById(R.id.radio_zan);
            this.radio_step = (TextView) rootView.findViewById(R.id.radio_step);
            this.radio_comment = (TextView) rootView.findViewById(R.id.radio_comment);
            this.radio_search = (TextView) rootView.findViewById(R.id.radio_search);
        }

    }
}
